// Copyright (c) 2025 Huawei Technologies Co., Ltd.
// openUBMC is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//         http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
//
 // Description: 提供对char*的拷贝，拼接等便利方法

 #include <algorithm>
 #include <stdexcept>
 #include <cstring>
 #include <boost/filesystem.hpp>
 #include <boost/algorithm/string.hpp>
 #include "Utils.hpp"
 
 // 删除整个char*的内存值
 static void ResetMemory(char*& str)
 {
     size_t len = strlen(str);
     if (len > 0) {
         std::fill_n(str, len, 0);
     }
 }
 
 char* DeepCopyCstr(const char* src)
 {
     try {
         if (src == nullptr) {
             throw std::invalid_argument("deep_copy_cstr: null pointer passed");
         }
 
         const size_t len = std::strlen(src) + 1;
         char* dest = new char[len];
         std::memcpy(dest, src, len * sizeof(char));
         return dest;
     } catch (...) {
         throw;
     }
 }
 
 void SafeStringUpdate(char*& upDest, const char* src)
 {
     try {
         if (upDest != nullptr) {
             ResetMemory(upDest);
             delete[] upDest;
             upDest = nullptr;
         }
 
         // 这种情况应直接调用DeleteCharArray，当SafeStringUpdate的src为nullptr则直接断定为异常
         if (src == nullptr) {
             DeleteCharArray(upDest);
             throw std::invalid_argument("SafeStringUpdate: null pointer passed");
         }
         upDest = DeepCopyCstr(src);
     } catch (...) {
         throw;
     }
 }
 
 void DeleteCharArray(char*& str) noexcept
 {
     if (str == nullptr) {
         return;
     }
     ResetMemory(str);
     delete[] str;
     str = nullptr;
 }
 
 char* SafeStrCat(const char* str1, const char* str2)
 {
     try {
         if (str1 == nullptr || str2 == nullptr) {
             throw std::invalid_argument("strcat: null pointer argument");
         }
         const size_t len1 = std::strlen(str1);
         const size_t len2 = std::strlen(str2);
         char* result = new char[len1 + len2 + 1];
         // 复制第一个字符串
         std::memcpy(result, str1, len1);
         // 复制第二个字符串及结尾\0
         std::memcpy(result + len1, str2, len2 + 1);
         return result;
     } catch (...) {
         throw;
     }
 }
 
 FileType GetFileType(const std::string& filePath) {
     try {
         boost::filesystem::path path(filePath);
 
         // 检查路径是否存在
         if (!boost::filesystem::exists(path)) {
             return FileType::INVALID;
         }
     
         // 检查是否是文件夹
         if (boost::filesystem::is_directory(path)) {
             return FileType::DIRECTORY;
         }
     
         // 检查是否是普通文件
         if (boost::filesystem::is_regular_file(path)) {
             // 检查文件是否为空
             if (boost::filesystem::file_size(path) == 0) {
                 return FileType::INVALID;
             }
 
             // 检查扩展名是否为 .iso（忽略大小写）
             std::string extension = path.extension().string();
             boost::algorithm::to_lower(extension); // 转换为小写
             if (path.extension() == ".iso") {
                 return FileType::ISO_IMAGE;
             }
             return FileType::REGULAR_FILE;
         }
     
         return FileType::INVALID;
     } catch (...) {
         throw;
     }
 }
 